﻿
using System;
using Test30.Core;

namespace Test30.Engine
{
    public abstract class Thing : IPosition, INoun
    {
        internal event EventHandler LightRadiusChanged;
        public abstract int LightRadius { get; }
        public abstract Dungeon Dungeon { get; }
        public abstract string Name { get; }
        public bool GivesOffLight => LightRadius > -1;
        public Vec Position
        {
            get => _pos;
            set
            {
                if (_pos != value) OnSetPosition(value);
            }
        }

        protected Thing(Vec pos)
        {
            _pos = pos;
        }
        public bool CanMove(Direction direction)
        {
            return CanOccupy(Position + direction);
        }
        public bool CanOccupy(Vec pos)
        {
            if (!Dungeon.Bounds.Contains(pos))
            {
                // out of the dungeon bounds
                return false;
            }

            if (!OnCanOccupy(pos, Dungeon.Tiles[pos]))
            {
                // impassable dungeon tile
                return false;
            }

            return true;
        }
        public void ForcePosition(Vec pos)
        {
            _pos = pos;
        }

        public abstract void Hit(Action action, Hit hit);

        protected virtual bool OnCanOccupy(Vec pos, Tile tile)
        {
            return tile.IsPassable;
        }

        protected void OnLightRadiusChanged()
        {
            LightRadiusChanged?.Invoke(this, EventArgs.Empty);
        }

        protected virtual void OnSetPosition(Vec pos)
        {
            _pos = pos;
        }
        #region INoun Members

        public abstract string NounText { get; }
        public abstract Person Person { get; }
        public abstract string Pronoun { get; }
        public abstract string Possessive { get; }

        #endregion

        private Vec _pos;
    }
}
